home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / xshp15.zip / XFPROJ.C < prev    next >
Text File  |  1991-12-26  |  2KB  |  57 lines

  1. /* Transforms all vertices in the specified polygon-based object into
  2.    view space, then perspective projects them to screen space and maps
  3.    them to screen coordinates, storing the results in the object.
  4.    Recalculates object->view transformation because only if the
  5.    transform changes would we bother to retransform the vertices.
  6.    Also recalculates CenterInView and moves the object through the
  7.    object list accordingly.
  8.  */
  9. #include <math.h>
  10. #include "polygon.h"
  11.  
  12. static Point3 CenterPoint = {0, 0, 0};
  13.  
  14. void XformAndProjectPObject(PObject * ObjectToXform)
  15. {
  16.    int i, NumPoints = ObjectToXform->NumVerts;
  17.    Point3 * Points = ObjectToXform->VertexList;
  18.    Point3 * XformedPoints = ObjectToXform->XformedVertexList;
  19.    Point3 * ProjectedPoints =
  20.          ObjectToXform->ProjectedVertexList;
  21.    Point * ScreenPoints = ObjectToXform->ScreenVertexList;
  22.  
  23.    /* Recalculate the object->view transform */
  24.    ConcatXforms(WorldViewXform, ObjectToXform->XformToWorld,
  25.          ObjectToXform->XformToView);
  26.  
  27.    /* Recalculate CenterInView */
  28.    XformVec(ObjectToXform->XformToView, (Fixedpoint *) &CenterPoint,
  29.          (Fixedpoint *) &ObjectToXform->CenterInView);
  30.  
  31.    /* Apply that new transformation and project the points */
  32.    for (i=0; i<NumPoints; i++, Points++, XformedPoints++,
  33.          ProjectedPoints++, ScreenPoints++) {
  34.       /* Transform to view space */
  35.       XformVec(ObjectToXform->XformToView, (Fixedpoint *) Points,
  36.             (Fixedpoint *) XformedPoints);
  37.  
  38.       /* Perspective-project to screen space */
  39.       ProjectedPoints->X =
  40.             FixedMul(FixedDiv(XformedPoints->X, XformedPoints->Z),
  41.             DOUBLE_TO_FIXED(PROJECTION_RATIO * (SCREEN_WIDTH/2)));
  42.       ProjectedPoints->Y =
  43.             FixedMul(FixedDiv(XformedPoints->Y, XformedPoints->Z),
  44.             DOUBLE_TO_FIXED(PROJECTION_RATIO * (SCREEN_WIDTH/2)));
  45.       ProjectedPoints->Z = XformedPoints->Z;
  46.  
  47.       /* Convert to screen coordinates. The Y coord is negated to
  48.          flip from increasing Y being up to increasing Y being down,
  49.          as expected by the polygon filler. Add in half the screen
  50.          width and height to center on the screen */
  51.       ScreenPoints->X = ((int) ((ProjectedPoints->X +
  52.             DOUBLE_TO_FIXED(0.5)) >> 16)) + SCREEN_WIDTH/2;
  53.       ScreenPoints->Y = (-((int) ((ProjectedPoints->Y +
  54.             DOUBLE_TO_FIXED(0.5)) >> 16))) + SCREEN_HEIGHT/2;
  55.    }
  56. }
  57.